Monkey portfolios

Monkey portfolios#

import pandas as pd
import numpy as np
from cvx.simulator import Builder
pd.options.plotting.backend = "plotly"
prices = pd.read_csv("data/stock-prices.csv", header=0, index_col=0, parse_dates=True)
b = Builder(prices=prices, initial_aum=1e6)

np.random.seed(42)
for time, state in b:
    n = len(state.assets)
    w = np.random.rand(n)
    # normalize the weights
    w = w/np.sum(w)
    
    assert np.all(w >= 0)
    assert np.allclose(np.sum(w), 1)

    b.weights = w 
    b.aum = state.aum
portfolio = b.build()
portfolio.nav.plot()
b = Builder(prices=prices, initial_aum=1e6)

np.random.seed(42)
for time, state in b:
    n = len(state.assets)
    w = np.random.rand(n)
    # normalize the weights
    w = w/np.sum(w)
    
    assert np.all(w >= 0)
    assert np.allclose(np.sum(w), 1)

    # multiply the shares with a constant portfolio size
    b.weights = w
    b.aum = state.aum
portfolio = b.build()
portfolio.snapshot(aggregate=True)